function explicit_error % Generates the data for plotting error at t=tmax when using explicit method % (a) for fixed N, and % (b) for fixed lambda % for the heat equation problem % diff(u,x,x) = diff(u,t) for xL < x < xr, 0 < t < tmax % where % u = 0 at x=xL,xR and u = sin(2*pi*x) at t = 0 % clear all previous variables and plots clear * clf % set parameters tmax=0.1; xL=0; xR=1; %%%%%% fixed N %%%%%%%%%%%%% N=20; % generate the points along the x-axis, x(1)=xL and x(N+2)=xR x=linspace(xL,xR,N+2); h=x(2)-x(1); ii=0; Mmax=3; Mmin=1; Nm=22; dm=(Mmax-Mmin)/(Nm-1); for i=1:Nm M=round(8.5*10^(Mmin+(i-1)*dm))-1; ii=ii+1; points(ii)=M+1; t=linspace(0,tmax,M+1); k=t(2)-t(1); lamda=k/h^2; error(ii)=explicit2(x,N+2,M+1,tmax,lamda); end; N=40; % generate the points along the x-axis, x(1)=xL and x(N+2)=xR x=linspace(xL,xR,N+2); h=x(2)-x(1); ii=0; Mmax=4; Mmin=2; Nm=22; dm=(Mmax-Mmin)/(Nm-1); for i=1:Nm M=round(3.5*10^(Mmin+(i-1)*dm))-1; ii=ii+1; points2(ii)=M+1; t=linspace(0,tmax,M+1); k=t(2)-t(1); lamda=k/h^2; error2(ii)=explicit2(x,N+2,M+1,tmax,lamda); end; % plot results % get(gcf) %set(gcf,'Position', [1235 552 615 415]); plotsize(615,415) subplot(2,1,2) loglog(points,error,'-sr','MarkerSize',7) hold on loglog(points2,error2,'-ob','MarkerSize',7) legend(' N = 20',' N = 40',3); % axes used in plot xlabel('Number of Time Points','FontSize',14,'FontWeight','bold') ylabel('Error','FontSize',14,'FontWeight','bold') % have MATLAB use certain plot options (all are optional) grid on set(gca,'MinorGridLineStyle','none') set(gca,'ytick',[1e-6 1e-5 1e-4 1e-3 1e-2]); set(findobj(gcf,'tag','legend'),'FontSize',14,'FontWeight','bold'); set(gca,'FontSize',14) hold off %%%%%% fixed lambda %%%%%%%%%%%%% N=18; M=73; ii=0; for i=1:5 ii=ii+1; points3(ii)=M; x=linspace(xL,xR,N+2); h=x(2)-x(1); t=linspace(0,tmax,M+1); k=t(2)-t(1); lamda=k/h^2; error3(ii)=explicit2(x,N+2,M+1,tmax,lamda); N=2*N+1; M=4*M; end; N=18; M=147; ii=0; for i=1:5 ii=ii+1; points4(ii)=M; x=linspace(xL,xR,N+2); h=x(2)-x(1); t=linspace(0,tmax,M+1); k=t(2)-t(1); lamda=k/h^2; error4(ii)=explicit2(x,N+2,M+1,tmax,lamda); N=2*N+1; M=4*M; end; % plot results subplot(2,1,1) loglog(points3,error3,'-sr','MarkerSize',7) hold on loglog(points4,error4,'-ob','MarkerSize',7) legend(' \lambda = 0.490',' \lambda = 0.245',3); % axes used in plot xlabel('Number of Time Points','FontSize',14,'FontWeight','bold') ylabel('Error','FontSize',14,'FontWeight','bold') % have MATLAB use certain plot options (all are optional) set(gca,'ytick',[1e-6 1e-5 1e-4 1e-3 1e-2]); set(findobj(gcf,'tag','legend'),'FontSize',14,'FontWeight','bold'); grid on set(gca,'MinorGridLineStyle','none') set(gca,'FontSize',14) hold off say=['Explicit Method: error when u(x,0)=sin(2\pix)']; title(say,'FontSize',14,'FontWeight','bold') % explicit method function q=explicit2(x,N,M,tmax,lamda) UE=zeros(N,M); for i=1:N UE(i,1)=g(x(i)); exact(i)=exp(-4*pi*pi*tmax)*sin(2*pi*x(i)); end; for j=2:M for i=2:N-1 UE(i,j)=lamda*UE(i+1,j-1)+(1-2*lamda)*UE(i,j-1)+lamda*UE(i-1,j-1); end; end; q=norm(UE(:,M)-exact',inf); % subfunction g(x) ' function q=g(x) q=sin(2*pi*x); % subfunction plotsize function plotsize(width,height) siz=get(0,'ScreenSize'); bottom=max(siz(4)-height-95,1); set(gcf,'Position', [2 bottom width height]);